package com.example.zhenyi;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class dbConnector {
    private static Connection conn;
    private static String url = "jdbc:sqlserver://10.10.10.13:2090;database=tjbg;trustServerCertificate=true";
    private static String userName = "sa2";
    private static String passWord = "topsky";


    String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

    public static Connection connect() throws SQLException {
        try{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
        }catch(ClassNotFoundException cnfe){
            System.err.println("Error: "+cnfe.getMessage());
        }catch(InstantiationException ie){
            System.err.println("Error: "+ie.getMessage());
        }catch(IllegalAccessException iae){
            System.err.println("Error: "+iae.getMessage());
        }

        conn = DriverManager.getConnection(url,userName,passWord);
        return conn;
    }
    public static Connection getConnection() throws SQLException, ClassNotFoundException{
        if(conn !=null && !conn.isClosed())
            return conn;
        connect();
        return conn;

    }
}